package com.sphd.beautysalon.activitys.push;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sphd.beautysalon.presenter.SetPushPresenterImpl;
import com.sphd.beautysalon.utils.AppManager;
import com.sphd.beautysalon.view.SetPushView;
import com.sphd.beautysalon.R;
import com.sphd.beautysalon.base.BaseActivity;
import com.sphd.beautysalon.base.RestaurantUrl;
import com.sphd.beautysalon.info.PushScaleInfo;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.utils.Constant;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class SetPushMoneyActivity extends BaseActivity implements SetPushView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.push_Recharge)
    TextView pushRecharge;
    @BindView(R.id.layout_Recharge)
    LinearLayout layoutRecharge;
    @BindView(R.id.push_Discount)
    TextView pushDiscount;
    @BindView(R.id.layout_Discount)
    LinearLayout layoutDiscount;
    @BindView(R.id.push_One)
    TextView pushOne;
    @BindView(R.id.layout_one)
    LinearLayout layoutOne;
    @BindView(R.id.content_set_push_money)
    RelativeLayout contentSetPushMoney;
    SetPushPresenterImpl setPushPresenter;
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    AlertDialog alertDialog;
    int TYPE = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_push_money);
        addActivity(this);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(view -> {
            if (Double.parseDouble(pushRecharge.getText().toString()) == 0
                    && Double.parseDouble(pushDiscount.getText().toString()) == 0 && Double.parseDouble(pushOne.getText().toString()) == 0) {
                AppManager.getAppManager().finishActivity(PushMoneyActivity.class);
                finishActivity(this);
            }else {
                setResult(0);
                finishActivity(this);
            }
        });
    }

    private void initView() {
        setTitleBar(toolbar, "提成比例设置");
        intent = getIntent();
        bundle = intent.getExtras();
        TYPE = bundle.getInt(Constant.TYPE);
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        setPushPresenter = new SetPushPresenterImpl(this);
        if (TYPE == 0) {
            addData();
        }
    }


    @OnClick({R.id.layout_Recharge, R.id.layout_Discount, R.id.layout_one})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_Recharge:
                updata("请输入充值提成比例", 2);
                break;
            case R.id.layout_Discount:
                updata("请输入打折消费提成比例", 1);
                break;
            case R.id.layout_one:
                updata("请输入一般消费提成比例", 0);
                break;
        }
    }

    public void updata(String message, final int type) {
        View view = View.inflate(this, R.layout.dialog_add, null);
        TextView title = (TextView) view.findViewById(R.id.title);
        title.setText(message);
        TextView mAccountMoney = (TextView) view.findViewById(R.id.m_account_money);
        mAccountMoney.setVisibility(View.GONE);
        final EditText mAddMoney = (EditText) view.findViewById(R.id.m_add_money);
        mAddMoney.setHint("");
        Button mSave = (Button) view.findViewById(R.id.m_save);
        Button mCancle = (Button) view.findViewById(R.id.m_cancle);
        alertDialog = new AlertDialog.Builder(this).setView(view).create();
        alertDialog.setCanceledOnTouchOutside(false);//使除了dialog以外的地方不能被点击
        alertDialog.show();
        mSave.setOnClickListener(v -> addMoney(mAddMoney.getText().toString(), alertDialog, type));
        mCancle.setOnClickListener(v -> alertDialog.dismiss());
    }

    private void addMoney(String msg, AlertDialog alertDialog, int type) {
        setPushPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.SETPUSH + "cgid=" + restaurantInfo.getId() +
                "&percent=" + msg + "&type=" + type, msg, type);
    }

    @Override
    public void recResOk(String ms, int type) {
        toast("操作已成功");
        if (type == 2) {
            pushRecharge.setText(ms);
        } else if (type == 1) {
            pushDiscount.setText(ms);
        } else if (type == 0) {
            pushOne.setText(ms);
        }

        alertDialog.dismiss();
    }

    @Override
    public void recResFaile() {
        toast("操作失败请重试。");
    }

    private void addData() {
        setPushPresenter.getSetVal(RestaurantUrl.Httptitle + RestaurantUrl.GETALREADYSETPUSH + "cgid=" + restaurantInfo.getId());
    }

    @Override
    public void recList(List<PushScaleInfo> pushScaleInfos) {

        for (PushScaleInfo scaleInfo : pushScaleInfos) {
            if (scaleInfo.getSalary_type() == 2) {
                pushRecharge.setText(String.valueOf(scaleInfo.getSalary_percent()));
            } else if (scaleInfo.getSalary_type() == 1) {
                pushDiscount.setText(String.valueOf(scaleInfo.getSalary_percent()));
            } else if (scaleInfo.getSalary_type() == 0) {
                pushOne.setText(String.valueOf(scaleInfo.getSalary_percent()));
            }
        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
